import { Tooltip } from "antd";
import type { TooltipProps } from "antd";
import React from "react";
import { Access, useAccess } from 'umi';

interface CustomAccessProps {
    code: () => void;
    fallback?: React.ReactNode;
    tooltip?: string | TooltipProps;
}

/**
 * 使用自定义权限
 * @param Component 
 * @returns 
 */
export function useCustomAccess<P>(Component: React.ComponentType<P>) {
    const { canOperatorItem } = useAccess();
    return ({ code, ...props }: P & CustomAccessProps) => (
        // 默认为不显示无权限组件
        <Access accessible={canOperatorItem(code)} fallback={props.fallback}>
            {props.tooltip ? <Tooltip {...typeof (props.tooltip) === 'string' ? { title: props.tooltip } : props.tooltip}>
                <Component {...props as P} />
            </Tooltip> : <Component {...props as P} />}
        </Access>
    )
}